<?php
namespace App\Admin\Renderable;
use App\Admin\Actions\Grid\GoodsTransferAction;
use App\Admin\Repositories\Shop\WxShopGoods;
use App\Models\Shop\WxShopClassify;
use App\Models\Shop\WxShopGoodsProduct;
use App\Wen\Utils\FieldUtils;
use Dcat\Admin\Grid;
use Dcat\Admin\Grid\LazyRenderable;

class ShopGoodsRender extends LazyRenderable
{
    public function grid(): Grid
    {
        $classfyids = WxShopClassify::pluck('name', 'id')->toArray();
        $classfyids[0] = '未分类';
        return Grid::make(new WxShopGoods(), function (Grid $grid) use ($classfyids){
            $grid->simplePaginate();
            $grid->quickSearch(['id', 'name'])->placeholder('搜索商品ID，名字...');
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('name')->limit(20);
            $grid->column('type')->using(FieldUtils::getShopGoodTypes())->sortable();
//            $grid->column('pic');
//            $grid->column('tag');
//            $grid->column('intro');
            $grid->column('vip_price')->sortable();
            $grid->column('price')->sortable();
//            $grid->column('content');
            $grid->column('views')->sortable()->editable();
            $grid->column('buys')->sortable()->editable();
            $grid->column('can_purchase_times', '限购次数')->sortable()->editable();
            $grid->column('rest_stock', '剩余库存')->display(function (){
                return WxShopGoodsProduct::where('goods_id', $this->id)->sum('stock');
            });
//            $grid->column('sku');

//            $grid->column('service_id');
            $grid->column('sort')->editable();
//            $grid->column('is_inspiration','灵感')->switch()->sortable();
            $grid->column('state')->switch()->sortable();
            $grid->column('classify_id')->sortable()->using($classfyids);
//            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) use ($classfyids){
                $filter->equal('id');
                $filter->equal('type')->multipleSelect(FieldUtils::getShopGoodTypes());
//                $filter->equal('classify_id')->multipleSelect($classfyids);
                $filter->like('name');
                $filter->notEqual('classify_id', '排除分类')->multipleSelect($classfyids);
            });
            $grid->batchActions(function ($batch) {
                $batch->add(new GoodsTransferAction());
            });

        });
    }
}
